package teshop.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import teshop.dao.CategoryDAO;
import teshop.dao.ProductDAO;
import teshop.domain.Category;
import teshop.domain.Product;

@Service
public class CatalogServiceImpl implements CatalogService {
	
	private ProductDAO productDAO;
	
	private CategoryDAO categoryDAO;
	
	@Autowired
	public CatalogServiceImpl(ProductDAO pProductDAO, CategoryDAO pCategoryDAO) {
		this.productDAO = pProductDAO;
		this.categoryDAO = pCategoryDAO;
	}

	@Override
	public List<Product> getProductsOfCategory(Long pCategoryId, int pPage, int pNumberOnPage) {
		return productDAO.getCategoryProducts(pCategoryId, pPage, pNumberOnPage);
	}

	@Override
	public void saveProduct(Product pProduct) {
		productDAO.persist(pProduct);
	}

	@Override
	public void saveCategory(Category pCategory) {
		categoryDAO.persist(pCategory);
	}

	@Override
	public List<Category> getSubCategories(Long pCategoryId) {
		return categoryDAO.getSubCategories(pCategoryId);
	}

	@Override
	public Category getCategory(Long pCategoryId) {
		return categoryDAO.loadById(pCategoryId);
	}

	@Override
	public List<Category> getAllCategories() {
		return categoryDAO.getAll();
	}

}
